package com.boot.down.util.redis;

import redis.clients.jedis.Jedis;

import static com.boot.down.util.redis.LockConstants.*;

/**
 * 加锁与解锁(释放锁) 测试三( 如果出现两个线程同时 for i++ .. 怎么处理, 就如存入的信息 肯能不同)
 * @author dell
 */
public class LockCase3 extends RedisLock {


	public LockCase3(Jedis jedis, String lockKey) {
		super(jedis, lockKey);
	}

	@Override
	public void lock() {
		while (true) {
			//设置锁的过期时间
			String result = jedis.set(lockKey, lockValue, NOT_EXIST, SECONDS,30);
			if (OK.equals(result)) {
				System.out.println(Thread.currentThread().getId() + "加锁成功!");
				break;
			}
		}
	}

	@Override
	public void unlock() {
		//解锁(释放锁)
		String lockValue = jedis.get(lockKey);
		if (lockValue.equals(this.lockValue)){
			jedis.del(lockKey);
		}
	}
}
